/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package buisness;

import dal.FriendsDAL;
import dal.UsersDAL;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import models.LineFriends;
import models.Users;

/**
 *
 * @author gabrielle
 */
public class Friends extends HttpServlet {


    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException 
    {
        //affichage des amis
        String username=(String)request.getSession().getAttribute("username");
        String search=(String)request.getSession().getAttribute("search");
        if(search==null)
        {
            if(username!=null)
            {
                Users user=UsersDAL.getUserByUsername(username);
                ArrayList<LineFriends> friends=FriendsDAL.getAllFriendsByUser(user);
                request.getSession().setAttribute("friends", friends);
                response.sendRedirect("./friends.jsp");
            }
            else
            {
                response.sendRedirect("./login.jsp");
            }
        }
        else
        {
            if(username!=null)
            {
                //rechercher un ami
                
                request.getSession().setAttribute("friend", UsersDAL.getUserByUsername(search));
                response.sendRedirect("./friends.jsp");
            }
            else
            {
                response.sendRedirect("./login.jsp");
            }
        }
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException 
    {
        /*ajouter un ami
        * on récupère les informations à ajouter donc l'username de l'ami 
        * on récupère 
        * ses infos dans la base
        * et on ajoute grâce au constructeur de linefriends dans models:
        * la date d'ajout est la date du jour
        * public LineFriends(Users user, Date addDate)
        */        
        String usernameOfFriend=request.getParameter("username");
        if(usernameOfFriend!=null)
        {
            //Users user=UsersDAL.getUserByUsername(usernameOfFriend);
            //LineFriends newFriend=new LineFriends(user, new Date(System.currentTimeMillis()));
            FriendsDAL.insertFriendInLineFriend(usernameOfFriend, new Date(System.currentTimeMillis()));
            response.sendRedirect("./friends.jsp");
        }
        else
        {
            response.sendRedirect("./login.jsp");
        }
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException 
    {
        //pour supprimer un ami on récupère l'utilisateur à supprimer 
        String usernameOfFriend=req.getParameter("username");
        if(usernameOfFriend!=null)
        {
            models.Friends friend= FriendsDAL.getFriendByUsername(usernameOfFriend);
            FriendsDAL.deleteFriends(friend);
              
        }
        else
        {
            resp.sendRedirect("./login.jsp");
        }
    }
   
}
